package social;

import static org.junit.Assert.*;

import java.util.*;

import message.*;

import org.junit.Test;


public class SocialDBCTest {
	private String un1 = "Roger";
	private String pw1 = "Hau";
	private String un2 = "Hung";
	private String pw2 = "Tran";
	private String un3 = "Evan";
	private String pw3 = "Plotkin";
	private String type = "user";
	private SocialDBC db = new SocialDBC();
	
	@Test
	public void smokeTest() {
		db.clearDatabase();
		db.addUser(un1, pw1, type);
		
		assertEquals(true, db.checkPassword("Roger", "Hau"));
		assertEquals(false, db.checkPassword("Roger", "Roger"));
		
		db.addUser(un2, pw2, type);
		db.addFriend(un1, un2);
		
		assertEquals(1, db.getFriendList(un1).size());
		assertEquals(un2, db.getFriendList(un1).get(0));
		
		db.clearDatabase();
	}
	
	@Test 
	public void friendTest() {
		db.clearDatabase();
		db.addUser(un1, pw1, type);
		db.addUser(un2, pw2, type);
		db.addUser(un3, pw3, type);
		db.addFriend(un1, un1);
		db.addFriend(un1, un2);
		db.addFriend(un1, un3);
		
		assertEquals(2, db.getFriendList(un1).size());
		assertEquals(1, db.getFriendList(un2).size());
		assertEquals(1, db.getFriendList(un3).size());
		
		db.removeFriend(un1, un2);
		assertEquals(1, db.getFriendList(un1).size());
		assertEquals(0, db.getFriendList(un2).size());
		assertEquals(1, db.getFriendList(un3).size());
		
		db.addFriend(un1, un2);
		db.addFriend(un3, un2);
		assertEquals(2, db.getFriendList(un1).size());

		db.removeFriend(un1, un2);
		db.removeFriend(un1, un3);
		db.removeFriend(un2, un3);
		assertEquals(0, db.getFriendList(un1).size());
		assertEquals(0, db.getFriendList(un2).size());
		//db.clearDatabase();
	}
	
	@Test
	public void adminTest() {
		db.addUser("admin", "admin", "admin");
		assertEquals(true, db.checkAdmin("admin"));
		assertEquals(false, db.checkAdmin(un1));
		db.promoteAdmin(un1);
		assertEquals(true, db.checkAdmin(un1));
		assertEquals(4, db.numUser());
	}
	
	@Test
	public void mailTest() {
		db.clearDatabase();
		Note note = new Note(un1, un2, "Hello World!");

		db.addUser(un1, pw1, type);
		db.addUser(un2, pw2, type);
		
		db.addMail(un1, un2, "Hello World!", "note");
		ArrayList<AbstractMessage> mailList = db.getMail(un1);
		assertEquals(1, mailList.size());
		assertEquals("Hello World!", mailList.get(0).getMessage());
		
		db.addMail(note);
		mailList = db.getMail(un1);
		assertEquals(2, mailList.size());
		assertEquals("Hello World!", mailList.get(0).getMessage());
		assertEquals("Hello World!", mailList.get(1).getMessage());
		
		ArrayList<Integer> mailIDs = db.getMailIdList(un1);
		//System.out.println(mailIDs.toString());
		assertEquals(1, mailIDs.get(0).intValue());
		assertEquals(2, mailIDs.get(1).intValue());
	}
	
	@Test
	public void requestTest() {
		db.clearDatabase();
		db.addUser(un1, pw1, type);
		db.addUser(un2, pw2, type);
		
		FriendRequest req = new FriendRequest(un2, un1, "Be my friend?");
		db.addMail(req);
		ArrayList<AbstractMessage> mailList = db.getMail(un2);
		assertEquals(1, mailList.size());
		assertEquals("request", mailList.get(0).getType());
		assertEquals(true, db.friendRequestSent(un2, un1));
	}
	
	@Test
	public void removeTest() {
		db.clearDatabase();
		db.addUser(un1, pw1, type);
		db.addUser(un2, pw2, type);
		
		Note note = new Note(un1, un2, "Hi!");
		db.addMail(note);
		ArrayList<AbstractMessage> mailList = db.getMail(un2);
		assertEquals(1, mailList.size());
		db.addFriend(un1, un2);
		db.removeUser(un1);
		assertEquals(false, db.userExists(un1));
		mailList = db.getMail(un2);
		assertEquals(0, mailList.size());
		assertEquals(0, db.getFriendList(un2).size());
	}
	
	@Test public void viewedTest() {
		db.clearDatabase();
		
		db.addUser("admin", "admin", "admin");
		db.addUser(un1, pw1, type);
		db.addUser(un2, pw2, type);
		db.addUser(un3, pw3, type);
		
		FriendRequest req = new FriendRequest(un2, un1);
		db.addMail(req);
		assertEquals(false, db.checkMailViewed(1));
		assertEquals(1, db.getUnreadMailIdList(un2).size());
		db.setMailViewed(1);
		assertEquals(true, db.checkMailViewed(1));

		//db.clearDatabase();
		

	}
//	@Test
//	public void mediumTest(){
//		db.clearDatabase();
//		db.addUser("Chuck", "Norris", "admin");
//		db.addUser(un1, pw1, type);
//		db.addUser(un2, pw2, type);
//		db.addUser(un3, pw3, type);
//
//		FriendRequest req = new FriendRequest(un2, un1);
//		db.addMail(req);
//		assertEquals(false, db.checkMailViewed(1));
//		db.setMailViewed(1);
//		assertEquals(true, db.checkMailViewed(1));
//	}
}
